//
//  BDUserAccount.m
//  HSY
//
//  Created by 陈浩 on 2017/4/12.
//  Copyright © 2017年 金开门. All rights reserved.
//

#import "BDUserAccount.h"

static BDUserAccount *user = nil;

@implementation BDUserAccount

+(instancetype) instatce{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        user = [[self alloc] init];
        [self restoreMyself];

    });
    return user;
}

+(id)allocWithZone:(struct _NSZone *)zone{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        user = [super allocWithZone:zone];
    });
    
    return user;
}

#if !__has_feature(objc_arc)

-(id)mutableCopyWithZone:(NSZone *)zone{
    return user;
}

-(id)copyWithZone:(NSZone *)zone{
    return user;
}

#endif

#pragma mark - MJExtension -
+(NSDictionary *)replacedKeyFromPropertyName{
    return @{@"auid" : @"id"};
}

static inline NSString * UserselfSaveFile(){
    return [NSHomeDirectory() stringByAppendingString:@"/Documents/BDUserAccount"];
}

+ (void)restoreMyself {
    NSString *path = UserselfSaveFile();
    // 从path路径找到并解档案
    [self objectWithContentsOfFile:path];
}
+ (void)saveMyselfToDocuments{
    NSString *path = UserselfSaveFile();
    BDUserAccount *userModel = [BDUserAccount instatce];
    [userModel writeToFile:path atomically:YES];
}

+ (void)removeSaveSelf{
    //移除文件
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL isHave = [fileManager fileExistsAtPath:UserselfSaveFile()];
    if (isHave) {
        [fileManager removeItemAtPath:UserselfSaveFile() error:nil];
    }
    
    //属性置空
    [BDUserAccount instatce].auid = 0;
    [BDUserAccount instatce].username = nil;
    [BDUserAccount instatce].realname = nil;
    [BDUserAccount instatce].email = nil;

}

@end
